home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-06-19 | 12.1 KB | 464 lines |
- package symantec.itools.awt;
-
- import java.awt.Color;
- import java.awt.Event;
- import java.awt.FlowLayout;
- import java.awt.LayoutManager;
- import java.awt.Panel;
- import java.awt.TextField;
- import java.awt.Graphics;
-
- // 01/29/97 TWB Integrated changes from Macintosh
- // 01/30/97 RKM Changed updateText to check if the text has changed, to avoid flicker
- // Also changed paint to first call updateText and removed ifdef MAC
-
- /**
- * This abstract class is used to create spinners. A spinner is a component
- * with two small direction buttons that lets the user scroll a list of
- * predetermined values to select one, or possibly enter a new legal value.
- * @see symantec.itools.awt.ListSpinner
- * @see symantec.itools.awt.NumericSpinner
- * @version 1.0, Nov 26, 1996
- * @author Symantec
- */
-
- public abstract class Spinner
- extends Panel
- implements Orientation
- {
- /**
- * The default orientation of the spinner buttons.
- */
- public final int ORIENTATION_DEFAULT = ORIENTATION_VERTICAL;
-
- /**
- * The minimum possible value.
- */
- protected int min;
- /**
- * The maximum possible value.
- */
- protected int max;
- /**
- * The current selection value. min <= current <= max.
- * @see #min
- * @see #max
- */
- protected int current;
- /**
- * The text of the current selection.
- */
- protected String text;
- /**
- * The width of the optional text field for this spinner.
- */
- protected int textWidth;
- /**
- * The optional text field for this spinner.
- */
- protected TextField textField;
- /**
- * Amount the current value chages when scrolling up or down.
- */
- protected int increment;
- private SpinButtonPanel buttons;
- private boolean editable;
- private boolean wrappable;
- private int orientation;
- private boolean textFieldAdded;
-
- /**
- * Construct an empty Spinner. It has the default orientation, an increment
- * of 1, no text field, and minimum, maximum, and current values are set to 0.
- */
- protected Spinner()
- {
- textFieldAdded = false;
- increment = 1;
- setWrappable(false);
- setMin(0);
- setMax(0);
- setCurrent(0);
- setOrientation(ORIENTATION_DEFAULT);
- textWidth = -1;
- super.setLayout(new FlowLayout());
- }
-
- /**
- * Conditionally enables editing of the Spinner's TextField.
- * @param f true = allow editing;
- * false = disallow editing
- */
- public void setEditable(boolean f)
- {
- editable = f;
-
- if (textFieldAdded)
- {
- setEditable();
- }
- }
-
- /**
- * Returns whether the Spinner's TextField is editable.
- * @return whether TextField editable, true = TextField can be edited;
- * false = TextField cannot be edited
- */
- public boolean getEditable()
- {
- return editable;
- }
-
- /**
- * Conditionally sets whether the current value wraps around from max
- * to min and from min to max.
- * @param f true if the value can wrap
- * @see #getWrappable
- */
- public void setWrappable(boolean f)
- {
- wrappable = f;
- }
-
- /**
- * Gets whether the current value can wrap around from max
- * to min and from min to max.
- * @return whether wrappable; f true if the value can wrap
- * @see #setWrappable
- */
- public boolean getWrappable()
- {
- return wrappable;
- }
-
- /**
- * Sets the minimum value the spinner may have.
- * @param i the new minimum value
- * @see #getMin
- */
- public void setMin(int i)
- {
- min = i;
- }
-
- /**
- * Gets the current minimum value the spinner may have.
- * @return the current minimum value
- * @see #setMin
- */
- public int getMin()
- {
- return min;
- }
-
- /**
- * Sets the maximum value the spinner may have.
- * @param i the new maximum value
- * @see #getMax
- */
- public void setMax(int i)
- {
- max = i;
- }
-
- /**
- * Gets the current maximum value the spinner may have.
- * @return the current maximum value
- * @see #setMax
- */
- public int getMax()
- {
- return max;
- }
-
- /**
- * Sets the current value of the spinner.
- * @param i the new current value
- * @see #getCurrent
- */
- public void setCurrent(int i)
- {
- current = i;
- updateText();
- }
-
- /**
- * Gets the current value of the spinner.
- * @return the current spinner value
- * @see #setCurrent
- */
- public int getCurrent()
- {
- return current;
- }
-
- /**
- * Sets the orientation of the spin buttons.
- * @param o the new orientation, ORIENTATION_VERTICAL or ORIENTATION_HORIZONTAL
- * @see #getOrientation
- */
- public void setOrientation(int o)
- {
- orientation = o;
-
- if (buttons != null)
- {
- remove(buttons);
- }
-
- switch (orientation)
- {
- case ORIENTATION_VERTICAL :
- {
- buttons = new VerticalSpinButtonPanel();
- break;
- }
-
- case ORIENTATION_HORIZONTAL :
- {
- buttons = new HorizontalSpinButtonPanel();
- break;
- }
- }
-
- if (textFieldAdded)
- {
- setStyle();
- }
- }
-
- private void setStyle()
- {
- add(buttons);
- }
-
- /**
- * Gets the current orientation of the spin buttons.
- * @return the current orientation, ORIENTATION_VERTICAL or ORIENTATION_HORIZONTAL
- * @see #setOrientation
- */
- public int getOrientation()
- {
- return orientation;
- }
-
- /**
- * Sets whether the spinner buttons will continually send notify messages
- * while pressed.
- * @param f true = send messages; false = do not send messages
- * @see #getNotifyWhilePressed
- * @see #setDelay
- * @see #getDelay
- */
- public void setNotifyWhilePressed(boolean f)
- {
- buttons.setNotifyWhilePressed(f);
- }
-
- /**
- * Returns the current notifyWhilePressed status.
- * @see #setNotifyWhilePressed
- * @see #setDelay
- * @see #getDelay
- */
- public boolean getNotifyWhilePressed()
- {
- return buttons.getNotifyWhilePressed();
- }
-
- /**
- * Sets the notification message delay of the spinner buttons in milliseconds.
- * @param d the delay between notification messages in milliseconds
- * @see #setNotifyWhilePressed
- * @see #getDelay
- */
- public void setDelay(int d)
- {
- buttons.setDelay(d);
- }
-
- /**
- * Returns the current delay between notification messages of the spinner
- * buttons in milliseconds
- * @see #setNotifyWhilePressed
- * @see #setDelay
- */
- public int getDelay()
- {
- return buttons.getDelay();
- }
-
- /**
- * Processes events for this component.
- * This is a standard Java AWT method which gets called by the AWT
- * to handle this component's events. The default handler for
- * components dispatches to one of the following methods as needed:
- * action(), gotFocus(), lostFocus(), keyDown(), keyUp(), mouseEnter(),
- * mouseExit(), mouseMove(), mouseDrag(), mouseDown(), or mouseUp().
- *
- * @param e the event to handle
- * @return true if the event was handled and no further action is needed,
- * false to pass the event to this component's parent
- * @see java.awt.Component#action
- * @see java.awt.Component#gotFocus
- * @see java.awt.Component#lostFocus
- * @see java.awt.Component#keyDown
- * @see java.awt.Component#keyUp
- * @see java.awt.Component#mouseEnter
- * @see java.awt.Component#mouseExit
- * @see java.awt.Component#mouseMove
- * @see java.awt.Component#mouseDrag
- * @see java.awt.Component#mouseDown
- * @see java.awt.Component#mouseUp
- */
- public boolean handleEvent(Event e)
- {
- switch(e.id)
- {
- case Event.SCROLL_PAGE_UP :
- {
- scrollUp();
- postEvent(new Event(this, Event.ACTION_EVENT, null));
- break;
- }
-
- case Event.SCROLL_PAGE_DOWN :
- {
- scrollDown();
- postEvent(new Event(this, Event.ACTION_EVENT, null));
- break;
- }
- }
-
- return super.handleEvent(e);
- }
-
- private void setEditable()
- {
- textField.setEditable(editable);
- }
-
- /**
- * Tells this component that it has been added to a container.
- * This is a standard Java AWT method which gets called by the AWT when
- * this component is added to a container. Typically, it is used to
- * create this component's peer.
- *
- * Here it's used to setup the component, creating the TextField as needed.
- * @see java.awt.Container#removeNotify
- */
- public void addNotify()
- {
- super.addNotify();
- if (!textFieldAdded) {
- textField = new TextField(textWidth > 0 ? textWidth - 1 : 10);
- textField.setBackground(Color.white);
- add(textField);
- textFieldAdded = true;
- }
- setEditable();
- setStyle();
- updateText();
- }
-
- /**
- * Paints this component using the given graphics context.
- * This is a standard Java AWT method which typically gets called
- * by the AWT to handle painting this component. It paints this component
- * using the given graphics context. The graphics context clipping region
- * is set to the bounding rectangle of this component and its <0,0>
- * coordinate is this component's top-left corner.
- *
- * @param g the graphics context used for painting
- * @see java.awt.Component#repaint
- * @see java.awt.Component#update
- */
- public void paint (Graphics g)
- {
- updateText();
-
- super.paint(g);
- }
-
- /**
- * Increments the spinner's value and handles wrapping as needed.
- */
- protected void scrollUp()
- {
- current += increment;
-
- if (current > max)
- {
- if (wrappable)
- {
- current = min;
- }
- else
- {
- current = max;
- }
- }
-
- updateText();
- }
-
- /**
- * Decrements the spinner's value and handles wrapping as needed.
- */
- protected void scrollDown()
- {
- current -= increment;
-
- if (current < min)
- {
- if (wrappable)
- {
- current = max;
- }
- else
- {
- current = min;
- }
- }
-
- updateText();
- }
-
- /**
- * Updates the text field with the current text, as needed.
- */
- protected void updateText()
- {
- if (textFieldAdded)
- {
- //If the text has changed, put the new text into the text field
- if (!textField.getText().equals(getCurrentText()))
- textField.setText(getCurrentText());
- }
- }
-
- /**
- * Gets the currently selected String from the list.
- * @return the String currently visible in the Spinner
- */
- protected abstract String getCurrentText();
-
- /**
- * Takes no action.
- * This is a standard Java AWT method which gets called to specify
- * which layout manager should be used to layout the components in
- * standard containers.
- *
- * Since layout managers CANNOT BE USED with this container the standard
- * setLayout has been OVERRIDDEN for this container and does nothing.
- *
- * @param l the layout manager to use to layout this container's components
- * (IGNORED)
- * @see java.awt.Container#getLayout
- **/
- public void setLayout(LayoutManager lm)
- {
- }
- }
-
-